package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.audi.R;
import com.audi.dto.MenuLeftItem;

public class MenuLeftAdapter extends BaseAdapter {

    private Activity mActivity;
    private ArrayList<MenuLeftItem> mListMenuItem;

    public MenuLeftAdapter(Activity activity,
	    ArrayList<MenuLeftItem> listMenuItem) {
	mActivity = activity;
	mListMenuItem = listMenuItem;
    }

    @Override
    public int getCount() {
	return mListMenuItem.size();
    }

    @Override
    public Object getItem(int arg0) {
	return mListMenuItem.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
	return arg0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int pos, View convertView, ViewGroup arg2) {
	ViewHolder viewHolder = null;
	if (convertView == null) {
	    viewHolder = new ViewHolder();
	    convertView = LayoutInflater.from(mActivity).inflate(
		    R.layout.menu_left_list_item, null);
	    viewHolder.imgIcon = (ImageView) convertView
		    .findViewById(R.id.img_icon);
	    viewHolder.txtLabel = (TextView) convertView
		    .findViewById(R.id.txtLabel);
	    viewHolder.txtNotification = (TextView) convertView
		    .findViewById(R.id.txt_notification);
	    viewHolder.rltContainer = (RelativeLayout) convertView
		    .findViewById(R.id.rlt_menu_container);
	    viewHolder.vBottomLine = (View) convertView
		    .findViewById(R.id.v_bottom_line);
	    convertView.setTag(viewHolder);
	} else {
	    viewHolder = (ViewHolder) convertView.getTag();
	}

	MenuLeftItem item = mListMenuItem.get(pos);

	viewHolder.rltContainer
		.setBackgroundResource(item.getBackgroundColor());
	viewHolder.txtLabel.setText(item.getTextLabel());

	viewHolder.imgIcon.setVisibility(View.VISIBLE);
	if (item.getIcon() > 0) {
	    viewHolder.imgIcon.setImageResource(item.getIcon());
	    viewHolder.imgIcon.setVisibility(View.VISIBLE);
	} else {
	    viewHolder.imgIcon.setVisibility(View.GONE);
	}

	if (!TextUtils.isEmpty(item.getTextNofitication().trim())) {
	    viewHolder.txtNotification.setText(item.getTextNofitication());
	    viewHolder.txtNotification.setVisibility(View.VISIBLE);
	} else {
	    viewHolder.txtNotification.setVisibility(View.GONE);
	}

	if (Integer.parseInt(item.getHasBottomLine()) == 1) {
	    viewHolder.vBottomLine.setVisibility(View.VISIBLE);
	} else {
	    viewHolder.vBottomLine.setVisibility(View.GONE);
	}

	return convertView;
    }

    static class ViewHolder {
	public ImageView imgIcon;
	public TextView txtLabel;
	public TextView txtNotification;
	public RelativeLayout rltContainer;
	public View vBottomLine;
    }
}
